package com.easychat.mychat.common.webSocket;


import com.easychat.mychat.common.entity.constants.Constants;
import com.easychat.mychat.common.entity.dto.MessageSendDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Description
 * @Author susu
 * @Date 2024/9/27
 */
@Component
@Slf4j
public class MessagePublisher {
    @Resource
    private RabbitTemplate rabbitTemplate;

    public void sendMsg(MessageSendDto messageSendDto){
        rabbitTemplate.convertAndSend(Constants.EXCHANGE_CHAT_TOPIC_NAME,Constants.ROUTING_KEY_CHAT,messageSendDto);
        log.info("消息已发送{}",messageSendDto);
    }

    public void sendMsgDown(MessageSendDto messageSendDto){

        rabbitTemplate.convertAndSend(Constants.EXCHANGE_CHAT_DELAYED, Constants.ROUTING_KEY_DELAYED, messageSendDto, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelay(Constants.MSG_DELAYED_TIME);
                return message;
            }
        });
        log.info("存入延迟消息{}",messageSendDto.getMessageId());
    }
}
